package com.yedean.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yedean.mall.api.CommonPage;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.entity.PmsProductAttributeCategory;
import com.yedean.mall.service.IPmsProductAttributeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * PmsProductAttributeCategoryController
 *
 * @author yedean
 * @date 2022/8/12
 */

@RestController
@Api(tags = "商品属性分类管理")
@RequestMapping("/admin/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private IPmsProductAttributeCategoryService productAttributeCategoryService;


    @ApiOperation("添加商品属性分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@RequestParam String name) {
        boolean save = productAttributeCategoryService.save(new PmsProductAttributeCategory().name(name));
        return CommonResult.success();
    }


    @ApiOperation("修改商品属性分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable Long id, @RequestParam String name) {
        productAttributeCategoryService.updateById(new PmsProductAttributeCategory().id(id).name(name));
        return CommonResult.success();
    }

    @ApiOperation("删除单个商品属性分类")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id) {
        boolean remove = productAttributeCategoryService.removeById(id);
        if (!remove) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

    @ApiOperation("获取单个商品属性分类信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProductAttributeCategory> getItem(@PathVariable Long id) {
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(id);
        return CommonResult.success(productAttributeCategory);
    }

    @ApiOperation("分页获取所有商品属性分类")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList(@RequestParam(defaultValue = "5") Integer pageSize,
                                                                         @RequestParam(defaultValue = "1") Integer pageNum) {
        IPage<PmsProductAttributeCategory> pageList = productAttributeCategoryService.getPageList(pageSize, pageNum);
        return CommonResult.success(CommonPage.resetPage(pageList));
    }
    
    


}
